# -*- coding: utf-8 -*-
import socket
import socks
import requests
from PIL import Image
from io import BytesIO
import sys, os
import json
import time
from multiprocessing import Pool
import urllib.parse
import itertools


def get_metadata(lat, lon):
    socks.setdefaultproxy(socks.SOCKS5, "127.0.0.1", 20170)
    socket.socket = socks.socksocket

    # location=25.1475961,121.4858669
    index = 'https://maps.googleapis.com/maps/api/streetview/metadata?'
    key = '&key=AIzaSyBlUJr1IlMi7AtbvJLkNG9r1m6awY8vguY'
    meta_url = index + f"location={lat},{lon}" + key
    # "status" : "OK"

    html = requests.get(meta_url)
    # if html.status_code == 200:
    #     # html_bytes = html.content
    #     html_str = html_bytes.decode()

    json_meta = html.json()
    print(f"{json_meta}\n{json_meta['status']}\n{meta_url}")
    if ("OK" == json_meta['status']):
        ret = True
    else:
        ret = False

    return ret


def solution2(url, fileName):
    try:
        # sess = requests.Session()
        # sess.mount('http://',HTTPAdapter(max_retries=3))
        # sess.mount('https://',HTTPAdapter(max_retries=3))
        # sees.keep_alive = False
        requests.packages.urllib3.disable_warnings()
        response = requests.get(url, stream=True, verify=False, timeout=(5, 5))
        # 保存
        with open(fileName, "wb") as f:  # 保存的文件名 保存的方式（wb 二进制  w 字符串）
            f.write(response.content)

        response.close()

    except Exception as e:
        print(e, url)


def readDatas():
    socks.setdefaultproxy(socks.SOCKS5, "127.0.0.1", 20170)
    socket.socket = socks.socksocket
    # json中获取坐标
    json_file = ''
    streets = return_dup()

    print(type(streets))
    path = "/home/wtgg/Downloads/liuzheng/Street_scene/SRC/pictures/Taipei/"  # 图片保存路径
    index = 'https://maps.googleapis.com/maps/api/streetview?size=640x640&'
    # Key = 'pitch=015&key=AIzaSyBlUJr1IlMi7AtbvJLkNG9r1m6awY8vguY'
    Key = 'pitch=015&key=AIzaSyBlUJr1IlMi7AtbvJLkNG9r1m6awY8vguY'

    # 统计该爬虫的消耗时间
    print('#' * 50)
    t1 = time.time()  # 开始时间

    print(f"{'+' * 5}len(streets){len(streets)},streets[:3]{streets[:3]}\n")

    # googleapi限制 单日请求次数25000
    cur = 0
    start = (cur) * 4000
    step = (cur + 1) * 4000
    for i in range(start, step):  # 1997903
        if (step > len(streets)):
            step = len(streets)

        lat = streets[i][0]
        lon = streets[i][1]

        # 判断坐标下是否有图片
        if (True != get_metadata(lat, lon)):
            # 记录到日志err.log
            continue
        else:
            path_pic = path + str(i) + f"_{lat}_{lon}"
            if (True == mkdir_single(path_pic)):

                for h in ['0', '60', '120', '180', '240', '300']:
                    filename = f"{path_pic}/{lat}_{lon}_{h}.jpeg"
                    img_url = index + f"location={lat},{lon}&heading={h}&" + Key
                    print(img_url)
                    # urllib.request.urlretrieve(img_url,filename=filename)
                    solution2(img_url, filename)

    t2 = time.time()  # 结束时间
    print('使用一般方法，总共耗时：%s' % (t2 - t1))
    print('#' * 50)


# # print(streets[0][0],streets[0][1])
# print(type(streets[0][0]))
# print(sys.getsizeof(streets) / 1024 / 1024, 'MB')

def return_dup():
    F_json_point = "/home/dev-2/002_work_2023/05_Street_scene/SRC/map/Taipei_all_private_0_40m_format.json"
    with open(F_json_point, encoding='utf-8-sig', errors='ignore') as f:
        Datas = json.load(f, strict=False)

        f.close()
    points = [i[2] for i in Datas["data"]]
    return points  # Datas["data"]


def check(point_list):
    data = []
    for i in point_list:
        if (0.002 < i[0] < 0.004):
            data.append(i[2])
    print(f" check(point_list){len(data)}")
    return data


def write_to_json(data, name):
    tmp_data = {}
    tmp_data["data"] = data
    tmp_json = json.dumps(tmp_data, ensure_ascii=False)
    with open(name, 'w+') as f:
        f.write(tmp_json)


# 创建单层目录
def mkdir_single(path):
    # 目录名称
    # basename:返回目录路径中的最后一个元素
    dirName = os.path.basename(path)
    # 判断路径是否存在
    isExists = os.path.exists(path)

    if not isExists:
        # 如果不存在，则创建单层目录
        os.mkdir(path)
        print('目录创建成功：' + dirName)
        return True
    else:
        # 如果目录存在则不创建，并提示目录已存在
        print('目录已存在：' + dirName)
        return False


if __name__ == '__main__':
    readDatas()

# get_metadata("25.0462092","121.5709866")
# return_dup()
